package com.whacos.modules.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whacos.common.annotations.Log;
import com.whacos.common.controller.BaseController;
import com.whacos.common.enums.Constant;
import com.whacos.common.utils.BeanUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.system.entity.SysRole;
import com.whacos.modules.system.service.SysRoleService;
import com.whacos.modules.system.vo.SysRoleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongdun
 * @description SysRoleController
 * @since 2018/11/17 17:11
 */
@Controller
@RequestMapping("/sys/role")
@Slf4j
public class SysRoleController extends BaseController {
    private static final String PREFIX = "sys/role";
    @Autowired
    private SysRoleService sysRoleService;

    // @Log("跳转到角色列表")
    @GetMapping()
    @RequiresPermissions("sys:role:role")
    public String role() {
        return PREFIX + "/role";
    }

    // @Log("查询角色列表")
    @GetMapping("/list")
    @ResponseBody()
    @RequiresPermissions("sys:role:role")
    public List<SysRoleVO> list(SysRoleVO sysRoleVO) {
        List<SysRoleVO> sysRoleVOList = new ArrayList<>();
        try {
            // 请求参数
            // 查询数据
            List<SysRole> sysRoleList = sysRoleService.list(Wrappers.lambdaQuery(new SysRole())
                    .eq(StringUtils.isNotEmpty(sysRoleVO.getName()), SysRole::getRoleName, sysRoleVO.getName())
                    .eq(StringUtils.isNotEmpty(sysRoleVO.getEnglishName()), SysRole::getRoleCode, sysRoleVO.getEnglishName())
                    .orderByAsc(SysRole::getCreatedBy));

            // 返回结果
            sysRoleList.forEach(sysRoleReturn -> {
                SysRoleVO sysRoleVOReturn = new SysRoleVO();
                BeanUtils.copyProperties(sysRoleReturn, sysRoleVOReturn);
                sysRoleVOList.add(sysRoleVOReturn);
            });
        } catch (Exception e) {
            log.error("查询角色列表异常，异常原因" + e.getMessage(), e);
        }
        return sysRoleVOList;
    }

    // @Log("添加角色信息")
    @GetMapping("/add")
    @RequiresPermissions("sys:role:add")
    public String add() {
        return PREFIX + "/add";
    }

    // @Log("跳转编辑角色信息")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("sys:role:edit")
    public String edit(@PathVariable("id") Long id, Model model) {
        try {
            SysRole sysRole = sysRoleService.getById(id);
            model.addAttribute("sysRole", sysRole);
        } catch (Exception e) {
            log.error("编辑角色信息异常，异常原因：" + e.getMessage(), e);
            return "error";
        }
        return PREFIX + "/edit";
    }

    @Log("保存角色信息")
    @PostMapping("/save")
    @ResponseBody()
    @RequiresPermissions("sys:role:add")
    public ResponseVO save(SysRoleVO sysRoleVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            SysRole sysRole = new SysRole();
            BeanUtils.copyProperties(sysRoleVO, sysRole);
            // 保存数据
            if (sysRoleService.save(sysRole)) {
                return ResponseVO.success();
            } else {
                return ResponseVO.error(1, "保存失败");
            }
        } catch (Exception e) {
            log.error("保存角色信息异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(1, "保存失败");
        }
    }

    @Log("更新角色信息")
    @PostMapping("/update")
    @ResponseBody()
    @RequiresPermissions("sys:role:edit")
    public ResponseVO update(SysRoleVO sysRoleVO) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            SysRole sysRole = new SysRole();
            BeanUtils.copyProperties(sysRoleVO, sysRole);

            if (sysRoleService.updateById(sysRole)) {
                return ResponseVO.success();
            } else {
                return ResponseVO.error(1, "保存失败");
            }
        } catch (Exception e) {
            log.error("保存角色信息异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(1, "保存失败");
        }
    }

    @Log("删除角色信息")
    @PostMapping("/remove")
    @ResponseBody()
    @RequiresPermissions("sys:role:remove")
    public ResponseVO remove(@RequestParam(value = "id") Long id) {
        if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            if (sysRoleService.removeById(id)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            log.error("删除角色信息异常，异常原因：" + e.getMessage(), e);
            return ResponseVO.error(1, "删除失败");
        }
        return ResponseVO.error(1, "删除失败");
    }

    @Log("批量删除角色")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("sys:role:batchRemove")
    public ResponseVO batchRemove(@RequestParam("ids[]") List<Long> ids) {
        if (Constant.DEMO_ACCOUNT.equals(this.getUsername())) {
            return ResponseVO.error(ResponseVO.ERROR_CODE, Constant.DEMO_DESCRIPTION);
        }
        try {
            if (sysRoleService.removeByIds(ids)) {
                return ResponseVO.success();
            }
        } catch (Exception e) {
            return ResponseVO.error(1, "删除失败");
        }
        return ResponseVO.error(1, "删除失败");
    }
}
